package com.yjz.zpay.service;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.yjz.zpay.R;
import com.yjz.zpay.utils.PayHelperUtils;
import com.yjz.zpay.utils.SpUtils;

import de.robv.android.xposed.XposedBridge;


/**
 * @author SuXiaoliang
 * @ClassName: DaemonService
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2018年6月23日 下午1:26:14
 */
public class DaemonService extends Service {
    public static String NOTIFY_ACTION = "com.tools.payhelper.notify";
    public static final int NOTICE_ID = 100;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        //如果API大于18，需要弹出一个可见通知
        String CHANNEL_ONE_ID = "com.yjz.zpay.setvice.DaemonService";
        String CHANNEL_ONE_NAME = "PpService";
        NotificationChannel notificationChannel = null;

        Notification.Builder builder = new Notification.Builder(this);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            XposedBridge.log("=>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
                    CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.enableLights(true);
            notificationChannel.setShowBadge(true);
            notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            manager.createNotificationChannel(notificationChannel);
            Notification notification = builder
                    .setChannelId(CHANNEL_ONE_ID)
                    .setContentTitle("支付监控")
                    .setContentText("支付监控正在运行中...")
                    .build();
            startForeground(NOTICE_ID, notification);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            builder.setSmallIcon(R.drawable.ic_launcher);
            builder.setAutoCancel(false);
            builder.setOngoing(true);
            startForeground(NOTICE_ID, builder.build());
        } else {
            startForeground(NOTICE_ID, new Notification());
        }
        PayHelperUtils.sendmsg(getApplicationContext(), "启动定时任务");

        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        int time = SpUtils.getInt(getApplicationContext(), "time");
        int triggerTime = 7 * 1000;
        if (time != 0) {
            triggerTime = time * 1000;
        }
        Intent i = new Intent(NOTIFY_ACTION);
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
        manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), triggerTime, pi);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 如果Service被终止
        // 当资源允许情况下，重启service
        return START_STICKY;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        // 如果Service被杀死，干掉通知
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            NotificationManager mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            mManager.cancel(NOTICE_ID);
        }
        // 重启自己
        Intent intent = new Intent(getApplicationContext(), DaemonService.class);
        startService(intent);
    }
}  